<script lang="ts">
import router from '@/router';
import { ref } from 'vue';
import { dynamicStore, publishStore, essayStore } from '../stores/publish'
import { onMounted } from 'vue';
import { toRaw } from 'vue';
import { watch } from 'vue';
import { showConfirmDialog } from 'vant';
import { computed } from 'vue';

export default {
    setup() {
        let goPage = () => {
            router.back();
        }
        let active = ref(0);

        let listitem: any = ref(null);

        let dy: any = computed(() => {
            return (dynamicStore().$state.dynamic).length
        })

        let pu: any = computed(() => {
            return (publishStore().$state.publish).length;
        })

        let es: any = computed(() => {
            return (essayStore().$state.essay).length;
        })


        watch(active, () => {
            getlist();
        })



        let getlist = () => {
            dy = toRaw(dynamicStore().$state.dynamic).length;
            pu = toRaw(publishStore().$state.publish).length;
            es = toRaw(essayStore().$state.essay).length;

            if (active.value == 0) {
                listitem.value = null;
                if (dy !== 0) {
                    listitem.value = toRaw(dynamicStore().$state.dynamic);
                }
            } else if (active.value == 1) {
                listitem.value = null;
                if (pu !== 0) {
                    listitem.value = toRaw(publishStore().$state.publish);
                }
            } else if (active.value == 2) {
                listitem.value = null;
                if (es !== 0) {
                    listitem.value = toRaw(essayStore().$state.essay);
                }
            }
        }

        let getgoPages = (index: any) => {
            if (active.value == 0) {
                router.push({ path: '/RreleaseView', query: { set: JSON.stringify(toRaw(dynamicStore().$state.dynamic)[index]) } })
            } else if (active.value == 1) {
                router.push({ path: '/Rreleaser', query: { set: JSON.stringify(toRaw(publishStore().$state.publish)[index]) } })
            } else if (active.value == 2) {
                router.push({ path: '/Rreleasers', query: { set: JSON.stringify(toRaw(essayStore().$state.essay)[index]) } })
            }

        }



        // position 为关闭时点击的位置
        const beforeClose: any = ({ name, position }: any) => {
            console.log(name);

            switch (position) {
                case 'left':
                case 'cell':
                case 'outside':
                    return true;
                case 'right':
                    return new Promise(() => {
                        showConfirmDialog({
                            title: '确定删除吗？',
                        }).then((resolve) => {
                            if (resolve) {
                                if (active.value == 0) {
                                    dynamicStore().$state.dynamic.splice(name, 1);
                                } else if (active.value == 1) {
                                    publishStore().$state.publish.splice(name, 1);
                                } else if (active.value == 2) {
                                    essayStore().$state.essay.splice(name, 1);
                                }
                            }
                        }).then(() => {
                            dy = toRaw(dynamicStore().$state.dynamic).length;
                        });
                    });
            }
            console.log(444);
        };


        onMounted(() => {
            getlist();
        })

        return {
            goPage,
            active,
            dy,
            pu,
            es,
            listitem,
            getgoPages,
            beforeClose

        }
    }
}
</script>

<template>
    <div class="diary">
        <div class="diary_title">
            <img @click="goPage" src="../assets/images/diary/com_taobao_nb_sdk_web_view_title_bar_back.9.png">
            草稿箱
        </div>
        <div class="diary_con">
            <span :class="active == 0 ? 'active' : ''" @click="active = 0">动态<em>{{ dy }}</em></span>
            <span :class="active == 1 ? 'active' : ''" @click="active = 1">日记<em>{{ pu }}</em></span>
            <span :class="active == 2 ? 'active' : ''" @click="active = 2">文章<em>{{ es }}</em></span>
        </div>
        <div class="diary_null" v-show="listitem == null">
            <img src="../assets/images/diary/empty_article.png">
            <p>暂无内容</p>
        </div>
        <div class="diary_list" v-show="listitem !== null">
            <div class="diary_item" v-show="active !== 2" v-for="(item, index) in listitem" :key="index"
                @click="getgoPages(index)">

                <van-swipe-cell :before-close="beforeClose" :name="index">
                    <van-cell :border="false">
                        <img :src="item.pics[0].content">
                        <div class="item_right">
                            <p class="one-txt-cut">{{ item.title == undefined ? "暂无内容" : item.title }}</p>
                            <p class="one-txt-cut">{{ item.content == "" ? "暂无内容" : item.content }}</p>
                        </div>
                    </van-cell>
                    <template #right>
                        <van-button square type="danger" text="删除" />
                    </template>
                </van-swipe-cell>

            </div>

            <div class="diary_items" v-show="active == 2" v-for="(item, index) in listitem" :key="index"
                @click="getgoPages(index)">
                <van-swipe-cell :before-close="beforeClose" :name="index">
                    <van-cell :border="false">
                        <p>{{ item.title == undefined ? "暂无内容" : item.title }}</p>
                        <div class="diary_bot">
                            <img :src="item.pics[0].content">
                            <p>{{ item.content == "" ? "暂无内容" : item.content }}</p>
                        </div>
                    </van-cell>
                    <template #right>
                        <van-button square type="danger" text="删除" />
                    </template>
                </van-swipe-cell>
            </div>
        </div>
        <div class="diary_end" v-show="listitem !== null">草稿在应用卸载后会被删除，请及时发布</div>
    </div>
</template>


<style lang="scss" scoped>
.diary_title {
    position: sticky;
    top: 0;
    left: 0;
    text-align: center;
    height: 40px;
    line-height: 40px;
    border-bottom: 1px solid #ccc;
    font-size: 16px;
    font-weight: 500;
    z-index: 10;
    background-color: #fff;

    img {
        width: 20px;
        height: 20px;
        display: block;
        position: absolute;
        top: 10px;
        left: 10px;
    }
}

.diary_con {
    padding: 0px 12px;
    position: sticky;
    top: 40px;
    left: 0;
    display: flex;
    z-index: 10;
    background-color: #fff;

    span {
        margin-right: 12px;
        padding-bottom: 6px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-bottom: 3px solid transparent;
        color: #999;

        &.active {
            color: #000;
            border-bottom-color: #000;
        }
    }
}

.diary_null {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 573px;
    background-color: #fff;

    img {
        width: 200px;
        height: 200px;
        display: block;
    }

    p {
        font-size: 14px;
        color: #999;
    }
}

.diary_item {
    padding: 10px 20px;
    display: flex;
    box-shadow: -2px 2px 2px 0px #e3e3e3;

    .van-swipe-cell {
        width: 100%;

        & ::v-deep .van-cell__value {
            display: flex !important;
        }

        .van-button {
            height: 100%;
        }

        img {
            width: 80px;
            height: 80px;
            display: block;
            margin-right: 10px;
        }

        .item_right {
            display: flex;
            flex-direction: column;
            justify-content: space-between;

            p {
                max-width: 249px;
            }

            p:first-child {
                font-size: 18px;
                font-weight: 600;
            }

            p:last-child {
                color: #999;
            }

        }
    }
}

.diary_items {
    margin: 10px 20px;
    padding: 18px 12px;
    border: 1px solid #999;

    .van-cell__value {
        &>p {
            text-align: start;
            font-size: 18px;
            font-weight: 600;
            color: #000;
        }
    }

    .van-button {
        height: 100%;
    }

    .diary_bot {
        display: flex;

        img {
            width: 80px;
            height: 80px;
            display: block;
            margin-right: 10px;
        }

        p {
            flex: 1;
            text-align: start;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 4;
        }
    }
}

.diary_end {
    margin-top: 30px;
    text-align: center;
    font-size: 12px;
    color: #999;
}
</style>